//
//  ZLHomeCell.swift
//  swiftDemo
//
//  Created by 众力金融 on 2018/8/28.
//  Copyright © 2018年 众力金融. All rights reserved.
//

import UIKit
import Kingfisher

class ZLHomeCell: UITableViewCell {

    var iconImageView: UIImageView = UIImageView()
    var nameLabel: UILabel = UILabel()
    var priceLabel: UILabel = UILabel()
    var originalLabel: UILabel = UILabel()
    var addBtn: UIButton = UIButton()
    
    typealias btnClick = (String) ->Void
    var  btnCLickBolock: btnClick?
    
    
    override func awakeFromNib() {
        super.awakeFromNib()
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
 
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: UITableViewCellStyle.default, reuseIdentifier: reuseIdentifier)
        setupUI()
    }
    
    var homeCellModel = recommendProductListModel() {
        didSet {
            let urlStr = "http://image.qxsoho.com/" + homeCellModel.imageUrl!
            iconImageView.kf.setImage(with: URL(string: urlStr))
            nameLabel.text = homeCellModel.productName
            if let salesPrice = homeCellModel.salesPrice {
                priceLabel.text = "¥ \(salesPrice)"
            }
            if let marketPrice = homeCellModel.marketPrice {
                let marketPriceStr = "原价:\(marketPrice)"
                let attributedText = NSMutableAttributedString(string: marketPriceStr)
                attributedText.addAttribute(NSAttributedStringKey.strikethroughStyle,
                                            value: NSUnderlineStyle.styleSingle.rawValue,
                                            range: NSRange(location: 0, length: attributedText.length))
                originalLabel.attributedText = attributedText
            }
            
        }
    }
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

extension ZLHomeCell {
    func setupUI() {
        
       self.contentView.addSubview(iconImageView)
        iconImageView.backgroundColor = UIColor.red
        iconImageView.snp.makeConstraints { (make) in
            make.left.right.top.equalTo(0);
            make.height.equalTo(150)
        }

        self.contentView.addSubview(nameLabel)
        nameLabel.text = "nihaoxiao"
        nameLabel.textColor = UIColor.black
        nameLabel.font = UIFont.systemFont(ofSize: 15)
      //  nameLabel.backgroundColor = UIColor.red
        nameLabel.snp.makeConstraints { (make) in
            make.top.equalTo(iconImageView.snp.bottom).offset(15);
            make.left.equalTo(15);
            make.right.equalTo(-15);
            make.height.equalTo(15)
        }

        self.contentView.addSubview(priceLabel)
        priceLabel.text = "12.00"
        priceLabel.textColor = UIColor.red
        priceLabel.font = UIFont.systemFont(ofSize: 15)
        priceLabel.snp.makeConstraints { (make) in
            make.left.equalTo(nameLabel.snp.left).offset(0)
            make.top.equalTo(nameLabel.snp.bottom).offset(10)
            make.height.equalTo(15)
        }

        self.contentView.addSubview(originalLabel)
        originalLabel.text = "12.00"
        originalLabel.textColor = UIColor.gray
        originalLabel.font = UIFont.systemFont(ofSize: 15)
        originalLabel.snp.makeConstraints { (make) in
            make.left.equalTo(priceLabel.snp.right).offset(15)
            make.top.equalTo(priceLabel.snp.top).offset(0)
            make.height.equalTo(15)
        }
        
        self.contentView.addSubview(addBtn)
        addBtn.backgroundColor = UIColor.red
        addBtn .addTarget(self, action: #selector(btnClickcell), for: .touchUpInside)
        addBtn.snp.makeConstraints { (make) in
            make.right.equalTo(-20);
            make.bottom.equalTo(-20);
            make.width.height.equalTo(30);
        }
        
        let lineView = UIView()
        lineView.backgroundColor = UIColor.colorHex(hex: "0xf4f4f4")
        self.contentView.addSubview(lineView)
        lineView.snp.makeConstraints { (make) in
            make.left.right.equalTo(0)
            make.bottom.equalTo(0)
            make.height.equalTo(10)
        }
    }
    
   @objc fileprivate func btnClickcell() {
    if btnCLickBolock != nil {
        btnCLickBolock!("你好")
    }
    }
}
